package org.example.leetcpde;

import org.junit.jupiter.api.Test;

public class searchInsert {

    @Test
    void run() {
        int[] nums = {1, 3, 5, 6};
        int i = searchInsert(nums, 2);
        System.out.println(i);
    }

    public int searchInsert(int[] nums, int target) {
        int i = 0;
        for (int len = nums.length, j = len - 1; i <= j; ) {
            int mid = (i + j) >> 1;
            int v = nums[mid];

            if (v == target) {
                return mid;
            }

            if (v > target) {
                j = mid - 1;
            } else {
                i = mid + 1;
            }
        }

        return i;
    }
}
